home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / ICMPHDR.C < prev    next >
Text File  |  1993-10-13  |  2KB  |  100 lines

  1. /* ICMP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "internet.h"
  7. #include "ip.h"
  8. #include "icmp.h"
  9.  
  10. /* Generate ICMP header in network byte order, link data, compute checksum */
  11. struct mbuf *
  12. htonicmp(struct icmp *icmp,struct mbuf *data)
  13. {
  14.     int16 checksum;
  15.     struct mbuf *bp = pushdown(data,ICMPLEN);
  16.     char *cp = bp->data;
  17.  
  18.     *cp++ = icmp->type;
  19.     *cp++ = icmp->code;
  20.     cp = put16(cp,0);        /* Clear checksum */
  21.  
  22.     switch(icmp->type){
  23.     case ICMP_DEST_UNREACH:
  24.         if(icmp->code == ICMP_FRAG_NEEDED) {
  25.             /* Deering/Mogul max MTU indication */
  26.             cp = put16(cp,0);
  27.             cp = put16(cp,icmp->args.mtu);
  28.         } else {
  29.             cp = put32(cp,0L);
  30.         }
  31.         break;
  32.     case ICMP_PARAM_PROB:
  33.         *cp++ = icmp->args.pointer;
  34.         *cp++ = 0;
  35.         cp = put16(cp,0);
  36.         break;
  37.     case ICMP_REDIRECT:
  38.         cp = put32(cp,icmp->args.address);
  39.         break;
  40.     case ICMP_ECHO:
  41.     case ICMP_ECHO_REPLY:
  42.     case ICMP_TIMESTAMP:
  43.     case ICMP_TIME_REPLY:
  44.     case ICMP_INFO_RQST:
  45.     case ICMP_INFO_REPLY:
  46.         cp = put16(cp,icmp->args.echo.id);
  47.         cp = put16(cp,icmp->args.echo.seq);
  48.         break;
  49.     default:
  50.         cp = put32(cp,0L);
  51.         break;
  52.     }
  53.     /* Compute checksum, and stash result */
  54.     checksum = cksum(NULLHEADER,bp,len_p(bp));
  55.  
  56.     cp = &bp->data[2];
  57.     cp = put16(cp,checksum);
  58.  
  59.     return bp;
  60. }
  61.  
  62. /* Pull off ICMP header */
  63. int
  64. ntohicmp(struct icmp *icmp,struct mbuf **bpp)
  65. {
  66.     char icmpbuf[ICMPLEN];
  67.  
  68.     if(icmp == NULL || pullup(bpp,icmpbuf,ICMPLEN) != ICMPLEN) {
  69.         return -1;
  70.     }
  71.     icmp->type = icmpbuf[0];
  72.     icmp->code = icmpbuf[1];
  73.  
  74.     switch(icmp->type){
  75.     case ICMP_DEST_UNREACH:
  76.         /* Retrieve Deering/Mogul MTU value */
  77.         if(icmp->code == ICMP_FRAG_NEEDED) {
  78.             icmp->args.mtu = get16(&icmpbuf[6]);
  79.         }
  80.         break;
  81.     case ICMP_PARAM_PROB:
  82.         icmp->args.pointer = icmpbuf[4];
  83.         break;
  84.     case ICMP_REDIRECT:
  85.         icmp->args.address = get32(&icmpbuf[4]);
  86.         break;
  87.     case ICMP_ECHO:
  88.     case ICMP_ECHO_REPLY:
  89.     case ICMP_TIMESTAMP:
  90.     case ICMP_TIME_REPLY:
  91.     case ICMP_INFO_RQST:
  92.     case ICMP_INFO_REPLY:
  93.         icmp->args.echo.id = get16(&icmpbuf[4]);
  94.         icmp->args.echo.seq = get16(&icmpbuf[6]);
  95.         break;
  96.     }
  97.     return 0;
  98. }
  99.  
  100.